#ifndef CONNECTION_H
#define CONNECTION_H
#include <QObject>
#include <QTcpSocket>

class Connection:public QTcpSocket
{
    Q_OBJECT
public:
    explicit Connection(QObject *parent = 0);
    bool tryConnect(const QString &hostName, quint16 port, OpenMode mode = ReadWrite);
    QString ipTo;
signals:
    void recieveData(QString ip,QByteArray data);
    void notify(QString ip,int code);
public slots:
    void onSendData(QByteArray data);
private slots:
    void onReadyRead();
    void onError(QAbstractSocket::SocketError errorCode);// emit signal notify

    void onConnected();
    void onDisconnected();
    void onBytesWritten(qint64);
private:
    int getDataNeedLen();
private:
    bool hasError;
    int curDataNeedLen;
    QByteArray dataReadBuffer;
};

#endif // CONNECTION_H
